package co.sugon.log.dao;

import co.sugon.log.searchEngineService.SQLExecuteService;
import co.sugon.log.utils.MYSQLUtil;
import co.sugon.log.utils.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;

public class AnalysisInterfaceDao {

    /**
     * 接口调用次数
     * */
    public static void interfaceCount(String interface_DateTime){

        String sql="select count(1) count,interface_name from  view_rz_to_hbase_table201710121728201  where dt='" + interface_DateTime + "' group by interface_name";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);

        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection=MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理

            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("interface_name")));
                pstmt.setString(2,StringUtils.isNUll(map.get("count")));
                pstmt.setString(3,interface_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }

    }


    /**
     * 接口活跃用户
     * */

    public static void interfaceUserCount(String interface_DateTime){

        String sql="select count(1) count,user_name from  view_rz_to_hbase_table201710121728201  where dt='" + interface_DateTime + "' group by user_name";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);

        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection=MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理
            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("user_name")));
                pstmt.setString(2,StringUtils.isNUll(map.get("count")));
                pstmt.setString(3,interface_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }

    }

    /**
     * 接口调用活跃单位
     * */
    public static void interfaceOrganizationCount(String interface_DateTime){

        String sql="select count(1) count,organization from  view_rz_to_hbase_table201710121728201  where dt='" + interface_DateTime + "' group by organization";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);

        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection=MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理
            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("organization")));
                pstmt.setString(2,StringUtils.isNUll(map.get("count")));
                pstmt.setString(3,interface_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }
    }

    /**
     * 接口调用成功失败次数 1 成功 0失败
     * */
    public static void interfaceResultCount(String interface_DateTime){

        String sql="select count(1) count,interface_result from  view_rz_to_hbase_table201710121728201  where dt='" + interface_DateTime + "' group by interface_result";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);

        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection=MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理
            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("interface_result")));
                pstmt.setString(2,StringUtils.isNUll(map.get("count")));
                pstmt.setString(3,interface_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }

    }





}
